<template>
  <div>
    <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments.js'
export default {
  name: '',
  components: {},
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const ruleCode = (rule, value, callback) => {
      const { originList, isEdit, id } = this
      // 点击编辑验证
      if (isEdit) {
        const editCode = originList.filter(item => item.id !== id)
        editCode.map(item => item.code).includes(value) ? callback(new Error('编码' + value + '已经存在')) : callback()
      }
      // 点击添加验证
      originList.map(item => item.code).includes(value) ? callback(new Error('编码' + value + '已经存在')) : callback()
    }
    const ruleName = (rule, value, callback) => {
      const { originList, id, isEdit } = this
      // 编辑
      if (isEdit) {
        const isObj = originList.find(item => item.id === id)
        const name2 = originList.filter(item => item.pid === isObj.pid).map(item => item.name)
        name2.filter(item => item !== isObj.name).includes(value) ? callback(new Error('名称' + value + '已经存在')) : callback()
      }
      //  添加
      const name = originList.filter(item => item.pid === id).map(item => item.name)
      name.includes(value) ? callback(new Error('名称' + value + '已经存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: ruleName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: ruleCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      employees: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.loadEmployee()
    this.loadDetail()
  },
  mounted() {},
  methods: {
    hCancel() {
      this.$emit('close')
    },
    async loadEmployee() {
      const rs = await getEmployeeSimple()
      // console.log('员工信息', rs)
      this.employees = rs.data
    },
    async doAdd() {
      // 去获取pid： 当前要添加的部门所在的父级的id
      // 从props中获取
      // 1. 组装参数
      const params = { ...this.form, pid: this.id }
      console.log(params)
      // 2. 调用接口
      const rs = await addDepartments(params)
      console.log(rs)
      // 3. 通知父组件：关闭弹层，再次更新数据
      this.$emit('success')
    },
    // 获取当前部门的详情
    async loadDetail() {
      if (this.isEdit) {
        // alert('现在是编辑，要去获取详情')
        const res = await getDepartDetail(this.id)
        console.log('现在是编辑，要去获取详情', res)
        this.form = res.data
      }
    },
    async doEdit() {
      try {
        await updateDepartments(this.form)
        this.$message({ type: 'success', message: '修改成功' })
        this.$emit('success')
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: err })
      }
    },
    hSubmit() {
      // 先省略表单验证
      this.$refs.deptForm.validate(valide => {
        if (valide) {
          // 根据 isEdit的值，决定做保存还是添加
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    }
  }
}
</script>

<style scoped lang='less'>

</style>
